package util;

import context.*;


import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.json.JSONArray;
import org.json.JSONException;


// ***************************************************************************
// *** CLASS: EditorProperties
// ***************************************************************************
public class EditorProperties implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="util.EditorProperties";

  private static final String WORKSPACE_DIRECTORY="WorkSpaceDirectory";
  private static final String FONT_FAMILY="Font.Family";
  private static final String FONT_STYLE="Font.Style";
  private static final String FONT_SIZE="Font.Size"; //
  private static final String FILL_COLOR="Fill.Color";
  private static final String FILL_RULE="Fill.Rule";
  private static final String STROKE_COLOR="Stroke.Color";
  private static final String STROKE_DASHARRAY="Stroke.Dasharray";
  private static final String STROKE_DASHOFFSET="Stroke.Dashoffset";
  private static final String STROKE_LINECAP="Stroke.Linecap";
  private static final String STROKE_LINEJOIN="Stroke.Linejoin";
  private static final String STROKE_MITERLIMIT="Stroke.Miterlimit";
  private static final String STROKE_WIDTH="Stroke.Width";
	
  private static final String defaultWorkSpace="/Application Data/MapEditor";
  private static Properties properties;
  private static boolean opened=false;
  
  static //
  { properties=new Properties();
    setDefaults();
    load(); // try read Property File
    opened=true;
  }

  public static String getUserName()
  { return(System.getProperty("user.name")); }

  public static String getUserHomeDirectory()
  { return(System.getProperty("user.home")); }

  public static String getWorkSpaceDirectory()
  { return(properties.getProperty(WORKSPACE_DIRECTORY)); }

  public static void setWorkSpaceDirectory(String workSpaceDirectory)
  { properties.setProperty(WORKSPACE_DIRECTORY,workSpaceDirectory);
    if(opened) store();
  }
  
  public static void loadFont(FontStyle font)
  {	font.setFontFamily(properties.getProperty(FONT_FAMILY));
	font.setFontStyle(FontStyle.fontStyle(properties.getProperty(FONT_STYLE)));
	font.setFontSize(Double.parseDouble(properties.getProperty(FONT_SIZE)));
  }
  
  public static void storeFont(FontStyle font)
  {	properties.setProperty(FONT_FAMILY,font.getFontFamily());
	properties.setProperty(FONT_STYLE,FontStyle.encodeFontStyle(font.getFontStyle()));
	properties.setProperty(FONT_SIZE,""+font.getFontSize());
    store();
  }
  
  public static void loadFill(FillStyle fill)
  { fill.setColor(Util2.decodeRGBA(properties.getProperty(FILL_COLOR)));
    fill.setRule(Util2.fillRule(properties.getProperty(FILL_RULE)));
  }
  
  public static void storeFill(FillStyle fill)
  { properties.setProperty(FILL_COLOR,Util2.encodeRGBA(fill.getColor()).toString());
	properties.setProperty(FILL_RULE,Util2.fillRule(fill.getRule()));
    store();
  }
  
  public static void loadStroke(StrokeStyle stroke)
  { stroke.setColor(Util2.decodeRGBA(properties.getProperty(STROKE_COLOR)));
    try { stroke.setDashArray(Util2.decodeFloats(new JSONArray(properties.getProperty(STROKE_DASHARRAY)))); }
    catch(JSONException ignore) {}
    stroke.setDashOffset(Float.parseFloat(properties.getProperty(STROKE_DASHOFFSET)));
    stroke.setLineCap(Util2.lineCap(properties.getProperty(STROKE_LINECAP)));
    stroke.setLineJoin(Util2.lineJoin(properties.getProperty(STROKE_LINEJOIN)));
    stroke.setMiterLimit(Float.parseFloat(properties.getProperty(STROKE_MITERLIMIT)));
    stroke.setStrokeWidth(Float.parseFloat(properties.getProperty(STROKE_WIDTH)));
  }
  
  public static void storeStroke(StrokeStyle stroke)
  { properties.setProperty(STROKE_COLOR,Util2.encodeRGBA(stroke.getColor()).toString());
	properties.setProperty(STROKE_DASHARRAY,Util2.encodeFloats(stroke.getDashArray()).toString());
	properties.setProperty(STROKE_DASHOFFSET,""+stroke.getDashOffset());
	properties.setProperty(STROKE_LINECAP,Util2.lineCap(stroke.getLineCap()));
	properties.setProperty(STROKE_LINEJOIN,Util2.lineJoin(stroke.getLineJoin()));
	properties.setProperty(STROKE_MITERLIMIT,""+stroke.getMiterLimit());
	properties.setProperty(STROKE_WIDTH,""+stroke.getStrokeWidth());
    store();
  }
  
  
  private static void setDefaults()
  { setWorkSpaceDirectory(getUserHomeDirectory()+defaultWorkSpace);  
    properties.setProperty(FONT_FAMILY,FontStyle.DEFAULT_FONT_FAMILY);
	properties.setProperty(FONT_STYLE,FontStyle.encodeFontStyle(FontStyle.DEFAULT_FONT_STYLE));
	properties.setProperty(FONT_SIZE,""+FontStyle.DEFAULT_FONT_SIZE);
	properties.setProperty(FILL_COLOR,Util2.encodeRGBA(FillStyle.DEFAULT_COLOR).toString());
	properties.setProperty(FILL_RULE,Util2.fillRule(FillStyle.DEFAULT_RULE));
    properties.setProperty(STROKE_COLOR,Util2.encodeRGBA(StrokeStyle.DEFAULT_COLOR).toString());
	properties.setProperty(STROKE_DASHARRAY,Util2.encodeFloats(StrokeStyle.DEFAULT_DASH_ARRAY).toString());
	properties.setProperty(STROKE_DASHOFFSET,""+StrokeStyle.DEFAULT_DASH_OFFSET);
	properties.setProperty(STROKE_LINECAP,Util2.lineCap(StrokeStyle.DEFAULT_LINE_CAP));
	properties.setProperty(STROKE_LINEJOIN,Util2.lineJoin(StrokeStyle.DEFAULT_LINE_JOIN));
	properties.setProperty(STROKE_MITERLIMIT,""+StrokeStyle.DEFAULT_MITER_LIMIT);
	properties.setProperty(STROKE_WIDTH,""+StrokeStyle.DEFAULT_WIDTH);
  }
  
  private static void load()
  { try
    { File dir=new File(getWorkSpaceDirectory()+"/MetaData");
      File file=new File(dir,"EditorProperties.txt");
      if(DEBUG) Log.d(TAG,"load: file="+file.getAbsolutePath()+", Name: "+file.getName()+".");
      dir.mkdirs();
      InputStream inputStream=new BufferedInputStream(new FileInputStream(file));
      properties.loadFromXML(inputStream);
      inputStream.close();
      if(DEBUG) properties.list(System.out);
    } catch(IOException e) { Log.e(TAG,"load",e); }
  }
  
  private static void store()
  { try
    { File dir=new File(getWorkSpaceDirectory()+"/MetaData");
      File file=new File(dir,"EditorProperties.txt");
      if(DEBUG) Log.d(TAG,"store: file="+file.getAbsolutePath()+", Name: "+file.getName()+".");
      if(DEBUG) properties.list(System.out);
      dir.mkdirs();
      OutputStream outputStream=new BufferedOutputStream(new FileOutputStream(file));
      properties.storeToXML(outputStream,"MapEditor general properties");
      outputStream.close();
    } catch(IOException e) { Log.e(TAG,"store",e); }
  }
}
